home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / wiconify / wiconcalls.lzh / wExample2 / wExample2.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  8KB  |  337 lines

  1. /*
  2.  *  WEXAMPLE2.C     Example program for wIconify.
  3.  *                  This program uses an icon with an IconPort to monitor
  4.  *                  most of the IconMessages classes.
  5.  */
  6.  
  7. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  8. #include <intuition/intuition.h>
  9.  
  10. #include "wIcon.h"
  11.  
  12. /*
  13.  *  The revision levels needed to the libraries
  14.  */
  15.  
  16. #define INTUITION_REV   0
  17. #define GRAPHICS_REV    0
  18. extern struct IntuitionBase *IntuitionBase;
  19. extern struct GfxBase *GfxBase;
  20.  
  21.  
  22. /*
  23.  *  Return codes for error and OK
  24.  */
  25.  
  26. #define ERROR_EXIT      10
  27. #define OK_EXIT         0
  28.  
  29.  
  30. /*
  31.  *  Some routines we need
  32.  */
  33.  
  34. extern struct Window *OpenWindow();
  35. extern struct Node *GetMsg();
  36. extern struct MsgPort *CreatePort();
  37. extern APTR OpenLibrary();
  38.  
  39.  
  40. /*
  41.  *  Types of IconMessage to report
  42.  */
  43.  
  44. #define REPORTFLAGS\
  45.   (WI_REPORTICONIFIED| WI_REPORTRESTORE| WI_REPORTSELECT| WI_REPORTUNSELECT|\
  46.    WI_REPORTPRESS| WI_REPORTRELEASE| WI_REPORTMOVED| WI_REPORTCLOSE|\
  47.    WI_REPORTNEWSCREEN| WI_REPORTSCREENCLOSE| WI_REPORTICONEND|\
  48.    WI_REPORTACTIVATE| WI_REPORTINACTIVE)
  49.  
  50. /*
  51.  *  The definition for our window
  52.  */
  53.  
  54. static struct NewWindow NewWindow =
  55. {
  56.    160,50, 320,100, 0,1, CLOSEWINDOW| REFRESHWINDOW,
  57.    WINDOWCLOSE| WINDOWDRAG| WINDOWDEPTH| WINDOWSIZING| SIMPLE_REFRESH,
  58.    NULL, NULL, "wExample2", NULL, NULL, 20,10, -1,-1, WBENCHSCREEN
  59. };
  60. static struct Window *myWindow;
  61.  
  62.  
  63. /*
  64.  *  This is the icon definition.  It uses the default imagery and flags,
  65.  *  but adds a lot of report flags.
  66.  */
  67.  
  68. static WICON myIcon =
  69. {
  70.    NULL,                /* No special name (default is the window name) */
  71.    NULL,NULL,NULL,      /* Use default Image, Select, and Mask */
  72.    0,0,                 /* Let wIconify place it where it wants */
  73.    0,                   /* No special flags  */
  74.    REPORTFLAGS,         /* The special report flags needed */
  75.    NULL                 /* We'll add the icon port once it is created */
  76. };
  77.  
  78.  
  79. /*
  80.  *  The Icon port
  81.  */
  82.  
  83. static struct MsgPort *IconPort;
  84.  
  85.  
  86.  
  87. /*
  88.  *  CleanupIconPort()
  89.  *
  90.  *  If the icon port was allocated
  91.  *    Reply any messages waiting in the port
  92.  *    Delete the port
  93.  */
  94.  
  95. static void CleanupIconPort()
  96. {
  97.    struct wIconMessage *theMessage;
  98.    
  99.    if (IconPort)
  100.    {
  101.       while (theMessage = (struct wIconMessage *)GetMsg(IconPort))
  102.          ReplyMsg(theMessage);
  103.       DeletePort(IconPort);
  104.    }
  105. }
  106.  
  107.  
  108. /*
  109.  *  DoExit()
  110.  *
  111.  *  Print an error message, if necessary, and then clean up any allocated
  112.  *  memory, close the windows and libraries, etc.  Finally, exit.
  113.  */
  114.  
  115. static void DoExit(s,x1,x2,x3)
  116. char *s, *x1,*x2,*x3;
  117. {
  118.    int status = OK_EXIT;
  119.  
  120.    if (s)
  121.    {
  122.       printf(s,x1,x2,x3);
  123.       printf("\n");
  124.       status = ERROR_EXIT;
  125.    }
  126.    if (myWindow)        CloseWindow(myWindow);
  127.    if (IconPort)        CleanupIconPort();
  128.    if (IntuitionBase)   CloseLibrary(IntuitionBase);
  129.    if (GfxBase)         CloseLibrary(GfxBase);
  130.    exit(status);
  131. }
  132.  
  133.  
  134. /*
  135.  *  CheckLibOpen()
  136.  *
  137.  *  Check to see if the specified library can be openned, and exit with
  138.  *  an error if not.
  139.  */
  140.  
  141. static void CheckLibOpen(lib,name,rev)
  142. APTR *lib;
  143. char *name;
  144. int rev;
  145. {
  146.    extern APTR OpenLibrary();
  147.    
  148.    if ((*lib = OpenLibrary(name,(ULONG)rev)) == NULL)
  149.       DoExit("Can't open '%s'",name);
  150. }
  151.  
  152.  
  153. /*
  154.  *  GetPort()
  155.  *
  156.  *  Create the port and attach it to the icon.
  157.  */
  158.  
  159. static void GetPort()
  160. {
  161.    IconPort = CreatePort(0,0);
  162.    if (IconPort == NULL) DoExit("Can't Create IconPort");
  163.    myIcon.IconPort = IconPort;
  164. }
  165.   
  166.  
  167. /*
  168.  *  OpenMyWindow()
  169.  *
  170.  *  Attempt to open the window (error if unsuccessful).
  171.  *  Add the icon to the window once it's open, then iconify the window.
  172.  */
  173.  
  174. static void OpenMyWindow()
  175. {
  176.    myWindow = OpenWindow(&NewWindow);
  177.    if (myWindow == NULL) DoExit("Can't open my window");
  178.    wSetIcon(myWindow,&myIcon);
  179.    wIconify(myWindow);
  180. }
  181.  
  182.  
  183. /*
  184.  *  DisplayMessage()
  185.  *
  186.  *  Set the pen color, and write out the message telling the user
  187.  *  what he should do with this window.
  188.  */
  189.  
  190. static void DisplayMessage()
  191. {
  192.    struct RastPort *rp = myWindow->RPort;
  193.  
  194.    SetAPen(rp,3);
  195.    Move(rp,110,43);
  196.    Text(rp,"Iconify Me!",11);
  197.    Move(rp,42,59);
  198.    Text(rp,"(Close me when you're done.)",28);
  199. }
  200.  
  201.  
  202. /*
  203.  *  WaitForAction()
  204.  *
  205.  *  While there is still more to do
  206.  *    Wait for a message to arrive at the window's UserPort
  207.  *    While there are more messages in the port
  208.  *      If the message class is a CLOSEWINDOW message, we're done
  209.  *      If its a REFRESH message, draw the text again.
  210.  *      Reply to the message
  211.  *    While there are more IconMessages in the port
  212.  *      Tell the user about the event
  213.  *      Reply to the message
  214.  */
  215.  
  216. static void WaitForAction()
  217. {
  218.    struct IntuiMessage *intuiMessage;
  219.    struct wIconMessage *iconMessage;
  220.    short NotDone = TRUE;
  221.    struct MsgPort *thePort = myWindow->UserPort;
  222.    long SignalMask = (1 << thePort->mp_SigBit) | (1 << IconPort->mp_SigBit);
  223.    WORD x,y;
  224.  
  225.    while (NotDone)
  226.    {
  227.       Wait(SignalMask);
  228.       while (intuiMessage = (struct IntuiMessage *)GetMsg(thePort))
  229.       {
  230.          switch(intuiMessage->Class)
  231.          {
  232.             case CLOSEWINDOW:
  233.                NotDone = FALSE;
  234.                break;
  235.  
  236.             case REFRESHWINDOW:
  237.                BeginRefresh(myWindow);
  238.                DisplayMessage();
  239.                EndRefresh(myWindow);
  240.                break;
  241.          }
  242.          ReplyMsg(intuiMessage);
  243.       }
  244.       while (iconMessage = (struct wIconMessage *)GetMsg(IconPort))
  245.       {
  246.          switch(iconMessage->Action)
  247.          {
  248.             case WI_REPORTICONIFIED:
  249.                printf(">> Window has been Iconified\n");
  250.                break;
  251.  
  252.             case WI_REPORTRESTORE:
  253.                printf(">> Window has been Restored\n");
  254.                break;
  255.  
  256.             case WI_REPORTSELECT:
  257.                printf(">> Icon Selected\n");
  258.                break;
  259.  
  260.             case WI_REPORTUNSELECT:
  261.                printf(">> Icon Unselected\n");
  262.                break;
  263.  
  264.             case WI_REPORTPRESS:
  265.                printf(">> Icon Pressed\n");
  266.                break;
  267.  
  268.             case WI_REPORTRELEASE:
  269.                printf(">> Icon Released\n");
  270.                break;
  271.  
  272.             case WI_REPORTMOVED:
  273.                wIconXY(iconMessage->Icon,&x,&y);
  274.                printf(">> Icon Moved To (%d,%d)\n",x,y);
  275.                break;
  276.  
  277.             case WI_REPORTCLOSE:
  278.                printf(">> Closing Icon\n");
  279.                wModifyReport(iconMessage->Icon,0);
  280.                NotDone = FALSE;
  281.                break;
  282.  
  283.             case WI_REPORTNEWSCREEN:
  284.                printf(">> New Screen\n");
  285.                break;
  286.  
  287.             case WI_REPORTSCREENCLOSE:
  288.                printf(">> Screen Closing\n");
  289.                break;
  290.  
  291.             case WI_REPORTACTIVATE:
  292.                printf(">> Iconify Backdrop Window Activated\n");
  293.                break;
  294.  
  295.             case WI_REPORTINACTIVE:
  296.                printf(">> Iconify Backdrop Window Inactive\n");
  297.                break;
  298.  
  299.             case WI_REPORTICONEND:
  300.                printf(">> Iconify Ending\n");
  301.                break;
  302.          }
  303.          ReplyMsg(iconMessage);
  304.       }
  305.    }
  306. }
  307.  
  308.  
  309. /*
  310.  *  main()
  311.  *
  312.  *  If wIconify is running
  313.  *    Open the libraries needed by the program
  314.  *    Create the IconPort
  315.  *    Open the window and add its icon
  316.  *    Display the message in the window
  317.  *    Wait for something to happen
  318.  *  Otherwise
  319.  *    Print an error message
  320.  *  Clean up afterwords
  321.  */
  322.  
  323. void main()
  324. {
  325.    if (wIconifyActive())
  326.    {
  327.       CheckLibOpen(&IntuitionBase,"intuition.library",INTUITION_REV);
  328.       CheckLibOpen(&GfxBase,"graphics.library",GRAPHICS_REV);
  329.       GetPort();
  330.       OpenMyWindow();
  331.       DisplayMessage();
  332.       printf("wExample2 window is open and iconified\n");
  333.       WaitForAction();
  334.    } else printf("wIconify not running or version mismatch\n");
  335.    DoExit(NULL);
  336. }
  337.